package com.tubruk.adapter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

import org.xmlpull.v1.XmlSerializer;

import android.util.Log;
import android.util.Xml;

import com.tubruk.object.TrackSummary;
import com.tubruk.trackme.MyGeoPoint;

public class KMLAdapter {
	
	public static final int KML_GENERATE_FAILED = -1;
	public static final int KML_GENERATE_SUCCESS = 1;
	
	/*
	 
	  <?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>Paths</name>
    <description>Examples of paths. Note that the tessellate tag is by default
      set to 0. If you want to create tessellated lines, they must be authored
      (or edited) directly in KML.</description>
    <Style id="yellowLineGreenPoly">
      <LineStyle>
        <color>7f00ffff</color>
        <width>4</width>
      </LineStyle>
      <PolyStyle>
        <color>7f00ff00</color>
      </PolyStyle>
    </Style>
    <Placemark>
      <name>Absolute Extruded</name>
      <description>Transparent green wall with yellow outlines</description>
      <styleUrl>#yellowLineGreenPoly</styleUrl>
      <LineString>
        <extrude>1</extrude>
        <tessellate>1</tessellate>
        <altitudeMode>absolute</altitudeMode>
        <coordinates> -112.2550785337791,36.07954952145647,2357
          -112.2549277039738,36.08117083492122,2357
          -112.2552505069063,36.08260761307279,2357
          -112.2564540158376,36.08395660588506,2357
          -112.2580238976449,36.08511401044813,2357
          -112.2595218489022,36.08584355239394,2357
          -112.2608216347552,36.08612634548589,2357
          -112.262073428656,36.08626019085147,2357
          -112.2633204928495,36.08621519860091,2357
          -112.2644963846444,36.08627897945274,2357
          -112.2656969554589,36.08649599090644,2357 
        </coordinates>
      </LineString>
    </Placemark>
  </Document>
</kml>
	 
	 */
	public static int generateKML(String filename, TrackSummary sum){
		int result = KMLAdapter.KML_GENERATE_FAILED;
		
		XmlSerializer serializer = Xml.newSerializer();
		FileWriter writer = null;
	    
	    try {
	    	
	    	File file = new File(filename);
			if (!file.exists()){
					file.createNewFile();
			}
	    	
	    	writer = new FileWriter(filename);
	        serializer.setOutput(writer);
	        serializer.startDocument("UTF-8", true);
	        serializer.startTag("http://www.opengis.net/kml/2.2", "kml");
	        serializer.startTag(null, "Document");
	        
	        //<name>Paths</name>
	        serializer.startTag(null, "name");
	        serializer.text("Paths");
	        serializer.endTag(null, "name");
	        
	        //<description>title</description>
	        serializer.startTag(null, "description");
	        serializer.text(sum.getTitle());
	        serializer.endTag(null, "description");
	  
	        serializer.startTag(null, "Style");
	        serializer.attribute(null, "id", "theStyle");
	        serializer.startTag(null, "LineStyle");
	        serializer.startTag(null, "color");
	        serializer.text("7f00ffff");
	        serializer.endTag(null, "color");
	        serializer.startTag(null, "width");
	        serializer.text("6");
	        serializer.endTag(null, "width");
	        serializer.endTag(null, "LineStyle");
	        serializer.startTag(null, "PolyStyle");
	        serializer.startTag(null, "color");
	        serializer.text("7f00ff00");
	        serializer.endTag(null, "color");
	        serializer.endTag(null, "PolyStyle");
	        serializer.endTag(null, "Style");
	        
	        serializer.startTag(null, "Placemark");
	        serializer.startTag(null, "name");
	        serializer.text(sum.getTitle());
	        serializer.endTag(null, "name");
	        serializer.startTag(null, "description");
	        serializer.text("TRAX: " + sum.getTitle());
	        serializer.endTag(null, "description");
	        serializer.startTag(null, "styleUrl");
	        serializer.text("#theStyle");
	        serializer.endTag(null, "styleUrl");
	        
	        serializer.startTag(null, "LineString");
	        serializer.startTag(null, "extrude");
	        serializer.text("1");
	        serializer.endTag(null, "extrude");
	        serializer.startTag(null, "tessellate");
	        serializer.text("1");
	        serializer.endTag(null, "tessellate");
	        serializer.startTag(null, "altitudeMode");
	        serializer.text("absolute");
	        serializer.endTag(null, "altitudeMode");
	        
	        List<MyGeoPoint> points = sum.getGeoPoints();
	        if (points != null){
	        	serializer.startTag(null, "coordinates");
		        
	        	int size = points.size();
	        	StringBuffer tmp = new StringBuffer();
	        	for (int i=0;i<size;i++){
	        		MyGeoPoint point = points.get(i);
	        		tmp.append(point.getLatitudeE6() / 1E6);
	        		tmp.append(",");
	        		tmp.append(point.getLongitudeE6() / 1E6);
	        		tmp.append(",2000\n");
	        	}
	        	serializer.text(tmp.toString());
	        	serializer.endTag(null, "coordinates");	
	        }
	        serializer.endTag(null, "LineString");
	        serializer.endTag(null, "Placemark");
	        
	        serializer.endTag(null, "Document");
	        
	        serializer.endTag("http://www.opengis.net/kml/2.2", "kml");
	        
	        serializer.endDocument();
	        
	    }catch (IOException e){
	    	Log.d("EXPORT KML","IOException: " + e.toString());
	    } catch (Exception e) {
	        Log.d("EXPORT KML", "Exception: " + e.toString());
	    } finally{
	    	if (writer != null){
	    		try {
					writer.close();
					result = KMLAdapter.KML_GENERATE_SUCCESS;
				} catch (IOException e) {
					Log.d("EXPORT KML","IOException OnClose: " + e.toString());
				}
	    	}
	    }
	    
	    return result;
	}

}
